package com.sf.springboot;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * 描述：
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年10月9日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class XMLWriter {
	public void updateXml(){
		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        Element environment=null, root=null;
        factory.setIgnoringElementContentWhitespace(true);
        
        DocumentBuilder db;
		try {
			db = factory.newDocumentBuilder();
			 Document xmldoc=db.parse(new File("D:\\workspace_study\\automaticSql\\src\\main\\resources\\dataStore.xml"));
		     root=xmldoc.getDocumentElement();
		     environment=(Element) selectSingleNode("/automaticSql/systems/system[systemCode='RPS']/environments/environment[environmentType='SIT']", root);
		     environment.getElementsByTagName("currentStatus").item(0).setTextContent("FAIL");
		     saveXml("D:\\workspace_study\\automaticSql\\src\\main\\resources\\dataStore.xml", xmldoc);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
	}
	
	public  Node selectSingleNode(String express, Object source) {//查找节点，并返回第一个符合条件节点
        Node result=null;
        XPathFactory xpathFactory=XPathFactory.newInstance();
        XPath xpath=xpathFactory.newXPath();
        try {
            result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        
        return result;
    }
	
	 public  void saveXml(String fileName, Document doc) {//将Document输出到文件
	        TransformerFactory transFactory=TransformerFactory.newInstance();
	        try {
	            Transformer transformer = transFactory.newTransformer();
	            transformer.setOutputProperty("indent", "yes");
	            DOMSource source=new DOMSource();
	            source.setNode(doc);
	            StreamResult result=new StreamResult();
	            result.setOutputStream(new FileOutputStream(fileName));
	            
	            transformer.transform(source, result);
	        } catch (TransformerConfigurationException e) {
	            e.printStackTrace();
	        } catch (TransformerException e) {
	            e.printStackTrace();
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        }   
	    }
	
	@Test
	public void testUpdate(){
		updateXml();
	}
}
